/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_SPRITE_IMPL__
#define __CC_PYTHON_IMPORT_O_SPRITE_IMPL__

#include "python_object_sprite.h"
#include "python_object_texture2d.h"
#include "python_object_textureatlas.h"
#include "python_object_spritebatchnode.h"
#include "python_object_spriteframe.h"

NS_CCPY_BEGIN
	CCSPRITE_OBJECT_WRAPPER_IMPL(CCSprite, CPySprite)	

	PYCLASS_INIT_AND_BASE_2(CPySprite, CPyNodeRGBA, CPyTextureProtocol, "CCSprite", "Python CCSprite Object");
	PYCLASS_BEGIN_METHOD_DEF(CPySprite)
	PYCLASS_ADD_METHOD(CPySprite, _updateColor)
	PYCLASS_ADD_METHOD(CPySprite, _setTextureCoords)
	PYCLASS_ADD_METHOD(CPySprite, _updateBlendFunc)
	PYCLASS_ADD_METHOD(CPySprite, _setReorderChildDirtyRecursively)
	PYCLASS_ADD_METHOD(CPySprite, _setDirtyRecursively)
	PYCLASS_ADD_METHOD_EX(CPySprite, setTextureRect, METH_KEYWORDS|METH_VARARGS)
	PYCLASS_ADD_METHOD(CPySprite, isFrameDisplayed)
	PYCLASS_ADD_METHOD(CPySprite, setDisplayFrameWithAnimationName)
	PYCLASS_END_METHOD_DEF(CPySprite)
	PYCLASS_BEGIN_GETSET_DEF(CPySprite)
	PYCLASS_ADD_GET(CPySprite, Quad)
	PYCLASS_ADD_GET(CPySprite, isTextureRectRotated)
	PYCLASS_ADD_GET(CPySprite, TextureRect)
	PYCLASS_ADD_GET(CPySprite, OffsetPosition)
	PYCLASS_ADD_GETSET(CPySprite, BatchNode)
	PYCLASS_ADD_SET(CPySprite, VertexRect)
	PYCLASS_ADD_GETSET(CPySprite, displayFrame)
	PYCLASS_ADD_GETSET(CPySprite, AtlasIndex)
	PYCLASS_ADD_GETSET(CPySprite, Dirty)
	PYCLASS_ADD_GETSET(CPySprite, TextureAtlas)
	PYCLASS_ADD_GETSET(CPySprite, FlipX)
	PYCLASS_ADD_GETSET(CPySprite, FlipY)
	PYCLASS_END_GETSET_DEF(CPySprite)
	PYCLASS_IMP_INTERFACE_NOINIT(CPySprite)

	int CPySprite::__init__(CPySprite *pySelf, PyObject *args, PyObject *kwds)
	{
		if( !args )
			return 0;
		static char* kwlist[] = {"fileName", "rect", "texture", "spriteFrame", "spriteFrameName", "rotated", NULL};
		char *filename = "";
		PyObject *py_rect = NULL;
		PyObject *py_texture = NULL;
		PyObject *py_spriteframe = NULL;
		char *spriteframename = "";
		bool rotated = false;
		if (!PyArg_ParseTupleAndKeywords(args,kwds,"|sOOOsb",kwlist, &filename, &py_rect, &py_texture, &py_spriteframe, &spriteframename, &rotated))
			return 0;
		cocos2d::CCSprite *spr = dynamic_cast<cocos2d::CCSprite *>(pySelf->getCCObject());
		if(!spr)
			return 0;
		if(filename && strlen(filename) > 0)
		{
			if(py_rect && py_rect != Py_None)
			{
				if(!PyObject_TypeCheck(py_rect, CPyClassBase::GetTypeObject<CPyRect>()))
					return 0;
				CPyRect *rect = (CPyRect *)py_rect;
				spr->initWithFile(filename, rect->getCCObject());
			}else
			{
				spr->initWithFile(filename);
			}
		}else if (py_texture && py_texture != Py_None)
		{
			if(!PyObject_TypeCheck(py_texture, CPyClassBase::GetTypeObject<CPyTexture2D>()))
				return 0;
			CPyTexture2D *texture = (CPyTexture2D *)py_texture;
			if(py_rect && py_rect != Py_None)
			{
				if(!PyObject_TypeCheck(py_rect, CPyClassBase::GetTypeObject<CPyRect>()))
					return 0;
				CPyRect *rect = (CPyRect *)py_rect;
				spr->initWithTexture((cocos2d::CCTexture2D *)texture->getCCObject(), rect->getCCObject(), rotated);
			}else
			{
				spr->initWithTexture((cocos2d::CCTexture2D *)texture->getCCObject());
			}
		}else if (py_spriteframe && py_spriteframe != Py_None)
		{
			if(!PyObject_TypeCheck(py_spriteframe, CPyClassBase::GetTypeObject<CPySpriteFrame>()))
				return 0;
			CPySpriteFrame *spriteframe = (CPySpriteFrame *)py_spriteframe;
			spr->initWithSpriteFrame((cocos2d::CCSpriteFrame *)spriteframe->getCCObject());
		}else if (spriteframename && strlen(spriteframename) > 0)
		{
			spr->initWithSpriteFrameName(spriteframename);
		}
		return 0;
	}
	PYCLASS_METHOD_IMPL(CPySprite, _updateColor)
	{
		CC_METHOD_WRAPPER_SETER(__CCSpriteWrapper *, CPySprite, g_updateColor);
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_IMPL(CPySprite, _setTextureCoords)
	{
		PyObject *pyrt = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &pyrt) )
			Py_RETURN_NONE;
		if(!PyObject_TypeCheck(pyrt, CPyClassBase::GetTypeObject<CPyRect>()))
			Py_RETURN_NONE;
		CPyRect *rt = (CPyRect *)pyrt;
		CC_METHOD_WRAPPER_SETER(__CCSpriteWrapper *, CPySprite, g_setTextureCoords, rt->getCCObject());
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_IMPL(CPySprite, _updateBlendFunc)
	{
		CC_METHOD_WRAPPER_SETER(__CCSpriteWrapper *, CPySprite, g_updateBlendFunc);
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_IMPL(CPySprite, _setReorderChildDirtyRecursively)
	{
		CC_METHOD_WRAPPER_SETER(__CCSpriteWrapper *, CPySprite, g_setReorderChildDirtyRecursively);
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_IMPL(CPySprite, _setDirtyRecursively)
	{
		PyObject *pyb = NULL;
		if( !PyArg_ParseTuple(pyArgs, "O", &pyb) )
			Py_RETURN_NONE;
		CC_METHOD_WRAPPER_SETER(__CCSpriteWrapper *, CPySprite, g_setDirtyRecursively, pyb == Py_True);
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_KW_IMPL(CPySprite, setTextureRect)
	{
		if( !pyArgs )
			Py_RETURN_NONE;
		static char* kwlist[] = {"rect", "rotated", "untrimmedSize", NULL};
		PyObject *rect = NULL;
		PyObject *rotated = NULL;
		PyObject *untrimmedSize = NULL;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"OOO",kwlist, &rect, &rotated, &untrimmedSize))
			Py_RETURN_NONE;
		if(!rect || rect == Py_None || !PyObject_TypeCheck(rect, CPyClassBase::GetTypeObject<CPyRect>()))
			Py_RETURN_NONE;
		bool b = rotated != Py_False;
		CPyRect *rt = (CPyRect *)rect;
		if(rotated && untrimmedSize || rotated != Py_None)
		{
			if(!PyObject_TypeCheck(untrimmedSize, CPyClassBase::GetTypeObject<CPySize>()))
				Py_RETURN_NONE;
			CPySize *si = (CPySize *)untrimmedSize;
			CC_METHOD_WRAPPER_SETER(CCSprite *, CPySprite, setTextureRect, rt->getCCObject(), b, si->getCCObject());
		}else
			CC_METHOD_WRAPPER_SETER(CCSprite *, CPySprite, setTextureRect, rt->getCCObject());
		Py_RETURN_NONE;
	}
	PYCLASS_METHOD_IMPL(CPySprite, isFrameDisplayed)
	{
		Py_RETURN_FALSE;
	}
	PYCLASS_METHOD_IMPL(CPySprite, setDisplayFrameWithAnimationName)
	{
		char *n = "";
		int i = 0;
		if( !PyArg_ParseTuple(pyArgs, "si", &n, &i) )
			Py_RETURN_NONE;
		CC_METHOD_WRAPPER_SETER(CCSprite *, CPySprite, setDisplayFrameWithAnimationName, n, i);
		Py_RETURN_NONE;
	}
	PYCLASS_GET_IMPL(CPySprite, Quad)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, getQuad, cocos2d::ccV3F_C4B_T2F_Quad);
		CPyV3F_C4B_T2F_Quad *v3f_Quad = CPyV3F_C4B_T2F_Quad::create(v);
		return (PyObject *)v3f_Quad;
	}
	PYCLASS_GET_IMPL(CPySprite, isTextureRectRotated)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, isTextureRectRotated, bool);
		Py_RETURN_BOOL(v);
	}
	PYCLASS_GET_IMPL(CPySprite, TextureRect)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, getTextureRect, cocos2d::CCRect);
		CPyRect *rt = CPyRect::create(v);
		return (PyObject *)rt;
	}
	PYCLASS_GET_IMPL(CPySprite, OffsetPosition)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, getOffsetPosition, cocos2d::CCPoint);
		CPyPoint *pt = CPyPoint::create(v);
		return (PyObject *)pt;
	}
	PYCLASS_GET_IMPL(CPySprite, BatchNode)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, getBatchNode, cocos2d::CCSpriteBatchNode *);
		CPySpriteBatchNode *bn = CPyCCObject::create<CPySpriteBatchNode>(v);
		return (PyObject *)bn;
	}
	PYCLASS_SET_IMPL(CPySprite, BatchNode)
	{
		if(!PyObject_TypeCheck(pyValue, CPyClassBase::GetTypeObject<CPySpriteBatchNode>()))
			return 0;
		CPySpriteBatchNode *bn = (CPySpriteBatchNode *)pyValue;
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setBatchNode, (cocos2d::CCSpriteBatchNode *)bn->getCCObject());
		return 0;
	}
	PYCLASS_SET_IMPL(CPySprite, VertexRect)
	{
		if(!PyObject_TypeCheck(pyValue, CPyClassBase::GetTypeObject<CPyRect>()))
			return 0;
		CPyRect *rt = (CPyRect *)pyValue;
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setVertexRect, rt->getCCObject());
		return 0;
	}
	PYCLASS_GET_IMPL(CPySprite, displayFrame)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, displayFrame, cocos2d::CCSpriteFrame *);
		CPySpriteFrame *sprf = CPyCCObject::create<CPySpriteFrame>(v);
		return (PyObject *)sprf;
	}
	PYCLASS_SET_IMPL(CPySprite, displayFrame)
	{
		if(!PyObject_TypeCheck(pyValue, CPyClassBase::GetTypeObject<CPySpriteFrame>()))
			return 0;
		CPySpriteFrame *sprf = (CPySpriteFrame *)pyValue;
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setDisplayFrame, (cocos2d::CCSpriteFrame *)sprf->getCCObject());
		return 0;
	}
	PYCLASS_GET_IMPL(CPySprite, AtlasIndex)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, getAtlasIndex, unsigned int);
		return Py_BuildValue("I", v);
	}
	PYCLASS_SET_IMPL(CPySprite, AtlasIndex)
	{
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setAtlasIndex, PyLong_AsUnsignedLong(pyValue));
		return 0;
	}
	PYCLASS_GET_IMPL(CPySprite, Dirty)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, isDirty, bool);
		Py_RETURN_BOOL(v);
	}
	PYCLASS_SET_IMPL(CPySprite, Dirty)
	{
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setDirty, pyValue != Py_False);
		return 0;
	}
	PYCLASS_GET_IMPL(CPySprite, TextureAtlas)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, getTextureAtlas, cocos2d::CCTextureAtlas *);
		CPyTextureAtlas *texa = CPyCCObject::create<CPyTextureAtlas>(v);
		return (PyObject *)texa;
	}
	PYCLASS_SET_IMPL(CPySprite, TextureAtlas)
	{
		if(!PyObject_TypeCheck(pyValue, CPyClassBase::GetTypeObject<CPyTextureAtlas>()))
			return 0;
		CPyTextureAtlas *texa = (CPyTextureAtlas *)pyValue;
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setTextureAtlas, (cocos2d::CCTextureAtlas *)texa->getCCObject());
		return 0;
	}
	PYCLASS_GET_IMPL(CPySprite, FlipX)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, isFlipX, bool);
		Py_RETURN_BOOL(v);
	}
	PYCLASS_SET_IMPL(CPySprite, FlipX)
	{
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setFlipX, pyValue != Py_False);
		return 0;
	}
	PYCLASS_GET_IMPL(CPySprite, FlipY)
	{
		CC_METHOD_WRAPPER_GETER(CCSprite *, CPySprite, isFlipY, bool);
		Py_RETURN_BOOL(v);
	}
	PYCLASS_SET_IMPL(CPySprite, FlipY)
	{
		CC_METHOD_WRAPPER_SETER_RINT(CCSprite *, CPySprite, setFlipY, pyValue != Py_False);
		return 0;
	}

NS_CCPY_END

#endif